<?php
/**
 * @copyright 2008 cryonyx
 */

require(dirname(__FILE__) . '/core/vc.options.php');

class VC_Options_WP implements VC_Options {
	/**
	 * Contains handle to database object
	 *
	 * @var VC_DB_WP
	 */
	private $dbo;

	/**
	 * Constructor of the object
	 *
	 * @param VC_DB $dbo
	 * @return VC_Options_WP
	 */
	public function __construct(VC_DB_WP &$dbo) {
		//	Store the reference to database object into local variable
		$this->dbo	=	$dbo;
	}
	
	/**
	 * Loads all options from database into session.
	 * 
	 * @param string $option Defines a specific option to fetch
	 * @return boolean
	 */
	public function fetch($option = NULL) {
		//	Checking if options have been already loaded into session
		if(	isset($_SESSION['vc'])		&&
			is_array($_SESSION['vc'])	&&
			$_SESSION['vc']['started']
		) {
			//	If yes, terminate the loading process to avoid excessive wasting of processor's time
			return TRUE;
		}
		
		//	Loading defaults
		$def_load_status	=	self::loadXMLFile();
		//	If loading is not successful, prepare session for storing options
		if(!$def_load_status) {
			$_SESSION['vc']	=	array();
		}
		//	Load parameters from database
		$params	=	$this->dbo->fetch_options($option);
		//	Checking whether options are loaded from database
		if(isset($params)) {
			//	If all is ok, store them in the session
			foreach($params as $key => $value) {
				$_SESSION['vc'][(string) $key]	=	(string) $value;
			}
			//	Set service initiation marker to TRUE. This marker signifies that VC is allowed to count statistics
			$_SESSION['vc']['started']	=	TRUE;
		} else {
			//	If some troubles occured with loading options from database, try using defaults
			trigger_error('VC: Options from database are not loaded. Trying to start with defaults;', E_USER_WARNING);
			if(!$def_load_status) {
				//	No options are loaded. VC cannot start
				$_SESSION['vc']['started']	=	FALSE;
				trigger_error('VC: Fatal error: cannot load parameters. Exiting;', E_USER_ERROR);
			} else {
				//	Defaults are used. Proceeding.
				$_SESSION['vc']['started']	=	TRUE;
				trigger_error('VC: Service started with default parameters. Please check VC installation;', E_USER_NOTICE);
			}
		}
		//	Returning the status of loading
		return $_SESSION['vc']['started'];
	}
	
	/**
	 * Stores all options from the session into database.
	 * 
	 * @param string $option Defines a specific option to store
	 * @return boolean
	 */
	public function store($option = NULL) {
		//	Checking whether VC is started
		if(!$_SESSION['vc']['started']) {
			//	Nothing to do
			return FALSE;
		}
		//	Preparing the array for storing
		$options	=	array();
		//	If specific option is defined, then store only it
		if(isset($option)) {
			//	Get specific option from the session
			$options[$option]	=	$_SESSION['vc'][$option];
		} else {
			//	Get all options from the session
			$options	=	array_diff($_SESSION['vc'], array('started'));
		}
		//	Storing options to database
		$result	=	$this->dbo->store_options($options);
		if(!$result){
			trigger_error('VC: Options were not saved. Please check your Wordpress settings;', E_USER_WARNING);
		} else {
			trigger_error('VC: VC settings have been updated;', E_USER_NOTICE);
		}
		
		return $result;
	}
	
	/**
	 * Loads a default settings' file.
	 *
	 * @return boolean
	 */
	public function loadXMLFile(){
		//	File is generally CMS-specific, thus it is placed in the same directory as the class
		$filename	=	dirname(__FILE__) . '/vc.vars.xml';
		//	Checking for file's existence
		if (file_exists($filename)) {
			//	Creating a SimpleXML object from file
		    $xml	=	simplexml_load_file($filename);
		    //	Avoiding a general problem of PHP XML parser (see: http://ru2.php.net/manual/ru/function.xml-parser-create.php#53188)
		    $p_cnt	=	count($xml->param);
		    //	Clearing the options array
		    $_SESSION['vc']	=	array();
		    //	Walking through the default parameters' array
		    for($i = 0; $i < $p_cnt; $i++) {
		    	//	Assigning current param to a temp. variable
		    	$param	=	$xml->param[$i];
		    	//	Storing parameter in the session
		    	$_SESSION['vc'][(string) $param->name]	=	(string) $param->value;
		    }
		    //	Signifying that parameters are loaded successfully
		    return TRUE;
		} else {
			//	Logging default parameters' loading failure
		    trigger_error('VC: vc.vars.xml was not found! Cannot load default parameters. Improper functioning is possible;', E_USER_WARNING);
		    //	Signifying that parameters are not loaded successfully
		    return FALSE;
		}
	}
}
?>